package com.chnbst.bstyun.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.model.pojo.AdminUser;
import com.chnbst.bstyun.model.pojo.StaticData;
import com.chnbst.bstyun.service.StaticDataService;

/**
 * 控制器注解 请求映射注解
 * controller 代表类是控制器
 * restcontroller 代表类是控制器， 所有的方法 都是ajax方法
 * @author zhangq
 */
@Controller
@RequestMapping("/sysAdmin")
public class StaticDataAdminController {
	
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private StaticDataService staticDataService;
	
	/**
	 * 数据字典维护
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/staticData/dataDicManager",method=RequestMethod.GET)
	public ModelAndView dataDicManager()throws Exception{
		return new ModelAndView("/admin/systemManager/dataDicManager");
	}
	
	/**
	 * Ajax 查询数据字典父类别列表
	 * @return 数据字典列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/staticData/staticDataTypeList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> staticDataTypeList(HttpServletRequest request) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		String staticDataCode = request.getParameter("staticDataCode");
		String staticDataName = request.getParameter("staticDataName");
		map.put("staticDataCode", staticDataCode);
		map.put("staticDataName", staticDataName);
		List<StaticData> dataTypeList = staticDataService.selectStaticDataByParentCode(map);
		
		map.put("dataTypeList", dataTypeList);
		return map;
	}
	
	/**
	 * Ajax 查询数据字典子类别列表
	 * @return 数据字典列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/staticData/staticDataList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> staticDataList(String id, HttpServletRequest request)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("parentCode", id);
		map.put("sortField", "static_data_num");
		map.put("sortOrder", "asc");
		List<StaticData> staticDataList = staticDataService.selectStaticDataListByParentCode(map);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("staticDataList", staticDataList);
		return resultMap;
	}

	/**
	 * 新增字典父类别  子类别
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/operate/staticData/addStaticData",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addStaticData(StaticData datas, HttpServletRequest request)throws Exception{
		AdminUser adminUser = (AdminUser) request.getSession().getAttribute("adminUser");
		Map<String, Object> map = new HashMap<String, Object>();
		boolean isSuccess = false;
		
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("staticDataCode", datas.getStaticDataCode());
		StaticData staticDataInfo = staticDataService.selectStaticDataByCondition(map1);
		
		//判断staticDataCode是否唯一，根据staticDataCode查找数据字典，如果数据为空则插入数据
		if(staticDataInfo == null){
			datas.setCreateId(adminUser.getAdminId());
			datas.setCreateTime(new Date());
			datas.setUpdateId(adminUser.getAdminId());
			datas.setUpdateTime(new Date());
			datas.setIsValid(1);
			//增加父类
			if(datas.getParentCode()==null){
				datas.setParentCode("-1");
				datas.setStaticDataNum(0);
				isSuccess = staticDataService.insertStaticData(datas);
			//增加子类	
			}else {
				isSuccess = staticDataService.insertStaticData(datas);
			}
		}else{
			map.put("message", "类型代码重复，请重新填写！");
		}
		map.put("isSuccess", isSuccess);
		return map;
	}
	
	/**
	 * 编辑字典父类别  子类别
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/operate/staticData/updateStaticData",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateStaticData(StaticData datas, HttpServletRequest request)throws Exception{
		AdminUser adminUser = (AdminUser) request.getSession().getAttribute("adminUser");
		Map<String, Object> map = new HashMap<String, Object>();
		boolean isSuccess = false;
		
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("staticDataCode", datas.getStaticDataCode());
		StaticData staticDataInfo = staticDataService.selectStaticDataByCondition(map1);
		
		staticDataInfo.setStaticDataName(datas.getStaticDataName());
		staticDataInfo.setUpdateId(adminUser.getAdminId());
		staticDataInfo.setUpdateTime(new Date());
		
		if("-1".equals(staticDataInfo.getParentCode())){
			isSuccess = staticDataService.updateStaticData(staticDataInfo);
		}else {
			staticDataInfo.setStaticDataNum(datas.getStaticDataNum());
			isSuccess = staticDataService.updateStaticData(staticDataInfo);
		}
		map.put("isSuccess", isSuccess);
		return map;
	}
	
	/**
	 * ajax请求 删除字典数据（多选删除父类别，子类别）
	 * @return 数据字典列表
	 */
	@RequestMapping(value="/operate/staticData/deleteStaticData",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteStaticData(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		AdminUser adminUser = (AdminUser) request.getSession().getAttribute("adminUser");
		String staticDataCodes = request.getParameter("ids"); 
		
		boolean isSuccess = false;
		if(staticDataCodes.contains(",")) {
			String[] staticDataId = staticDataCodes.split(",");
			for(int i=0;i<staticDataId.length;i++){
				Map<String, Object> map1 = new HashMap<String, Object>();
				map1.put("staticDataCode", staticDataId[i]);
				StaticData staticData = staticDataService.selectStaticDataByCondition(map1);
				//如果删除数据为父类则先删除它下面的子类
				if("-1".equals(staticData.getParentCode())){
					List<StaticData> staticDataList = staticDataService.selectStaticDataByStaticDataCode(staticDataId[i]);
					for(int j=0;j<staticDataList.size();j++){
						StaticData staticData1 = staticDataList.get(j);
						staticData1.setUpdateId(adminUser.getAdminId());
						staticData1.setUpdateTime(new Date());
						staticData1.setIsValid(0);
						isSuccess = staticDataService.updateStaticData(staticData1);
					}
				}
				staticData.setUpdateId(adminUser.getAdminId());
				staticData.setUpdateTime(new Date());
				staticData.setIsValid(0);
				
				isSuccess = staticDataService.updateStaticData(staticData);
				map.put("message", "删除成功！！");
			}
		}else {
			Map<String, Object> map1 = new HashMap<String, Object>();
			map1.put("staticDataCode", staticDataCodes);
			StaticData staticData = staticDataService.selectStaticDataByCondition(map1);
			//如果删除数据为父类则先删除它下面的子类
			if("-1".equals(staticData.getParentCode())){
				//子类
				List<StaticData> staticDataList = staticDataService.selectStaticDataByStaticDataCode(staticDataCodes);
				//for(StaticData staticData1 : staticData1){
				for(int j=0;j<staticDataList.size();j++){
					StaticData staticData1 = staticDataList.get(j);
					staticData1.setUpdateId(adminUser.getAdminId());
					staticData1.setUpdateTime(new Date());
					staticData1.setIsValid(0);
					isSuccess = staticDataService.updateStaticData(staticData1);
				}
			}
			staticData.setUpdateId(adminUser.getAdminId());
			staticData.setUpdateTime(new Date());
			staticData.setIsValid(0);
			
			isSuccess = staticDataService.updateStaticData(staticData);
			map.put("message", "删除成功！！");
		}
		map.put("isSuccess", isSuccess);
		return map;
	}
}